﻿Imports System
Imports System.Xml
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Configuration
Imports System.Data

Imports DotNetNuke
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Services.Search
Imports DotNetNuke.Services.Localization



Namespace dnnWerk.Modules.Nuntio.Content

    Public Class ContentController

        Implements Entities.Modules.ISearchable
        Implements Entities.Modules.IPortable
        Implements Entities.Modules.IUpgradeable


        Private Const MAX_DESCRIPTION_LENGTH As Integer = 500

#Region "Public Methods"

        Public Function GetHighestPriority(ByVal ModuleID As Integer) As Integer
            Return CType(DotNetNuke.Data.DataProvider.Instance().ExecuteScalar("Nuntio_Content_GetHighestPriority", ModuleID), Integer)
        End Function

        Public Sub UpdatePriority(ByVal ContentId As Integer, ByVal Priority As Integer)
            DotNetNuke.Data.DataProvider.Instance().ExecuteNonQuery("Nuntio_Content_UpdatePriority", ContentId, Priority)
        End Sub

        Public Function ListContent(ByVal ModuleId As Integer) As ContentCollection

            Dim lst As New List(Of ContentInfo)
            lst = CBO.FillCollection(Of ContentInfo)(DotNetNuke.Data.DataProvider.Instance().ExecuteReader("Nuntio_Content_List", ModuleId), lst)

            Dim coll As New ContentCollection
            coll.SetList(lst)

            Return coll

        End Function

        Public Function AddContent(ByVal objContent As ContentInfo) As Integer

            Dim ContentId As Integer = CType(DotNetNuke.Data.DataProvider.Instance().ExecuteScalar("Nuntio_Content_Add", objContent.ModuleId, objContent.Locale, objContent.Content, DotNetNuke.Data.DataProvider.Instance().GetNull(objContent.Title), objContent.Role, objContent.Priority, objContent.CreatedBy, objContent.CreatedDate, objContent.IsPublished, objContent.IsFallback, objContent.Version), Integer)

            Dim objLog As New ContentLogInfo
            objLog.Comment = Constants.CONTENT_ADDED
            objLog.ContentId = ContentId
            objLog.CreatedBy = objContent.CreatedBy
            objLog.CreatedDate = objContent.CreatedDate
            objLog.UserComment = ""
            objLog.ModuleId = objContent.ModuleId

            AddLogEntry(objLog)

            Return ContentId

        End Function

        Public Function AddContent(ByVal objContent As ContentInfo, ByVal UserId As Integer, ByVal UserComment As String) As Integer

            Dim ContentId As Integer = CType(DotNetNuke.Data.DataProvider.Instance().ExecuteScalar("Nuntio_Content_Add", objContent.ModuleId, objContent.Locale, objContent.Content, DotNetNuke.Data.DataProvider.Instance().GetNull(objContent.Title), objContent.Role, objContent.Priority, objContent.CreatedBy, objContent.CreatedDate, objContent.IsPublished, objContent.IsFallback, objContent.Version), Integer)

            Dim objLog As New ContentLogInfo
            objLog.Comment = Constants.CONTENT_NEWVERSION
            objLog.ContentId = ContentId
            objLog.CreatedBy = objContent.CreatedBy
            objLog.CreatedDate = objContent.CreatedDate
            objLog.UserComment = UserComment
            objLog.ModuleId = objContent.ModuleId

            AddLogEntry(objLog)

            Return ContentId

        End Function


        Public Sub UpdateContent(ByVal objContent As ContentInfo, ByVal UserId As Integer, ByVal UserComment As String, ByVal AddLog As Boolean)

            Dim ContentId As Integer = objContent.ContentId

            DotNetNuke.Data.DataProvider.Instance().ExecuteNonQuery("Nuntio_Content_Update", objContent.ContentId, objContent.ModuleId, objContent.Locale, objContent.Content, DotNetNuke.Data.DataProvider.Instance().GetNull(objContent.Title), objContent.Role, objContent.Priority, objContent.CreatedBy, objContent.CreatedDate, objContent.IsPublished, objContent.IsFallback, objContent.Version)

            If AddLog Then

                Dim objLog As New ContentLogInfo

                If objContent.IsPublished Then
                    objLog.Comment = Constants.CONTENT_MADE_VISIBLE
                Else
                    objLog.Comment = Constants.CONTENT_MADE_HIDDEN
                End If

                objLog.ContentId = ContentId
                objLog.CreatedBy = UserId
                objLog.CreatedDate = Date.Now
                objLog.UserComment = UserComment
                objLog.ModuleId = objContent.ModuleId

                AddLogEntry(objLog)

            End If

        End Sub

        Public Sub DeleteContent(ByVal ContentId As Integer)
            DotNetNuke.Data.DataProvider.Instance().ExecuteNonQuery("Nuntio_Content_Delete", ContentId)
        End Sub

        Public Function AddLogEntry(ByVal objLog As ContentLogInfo) As Integer

            Return CType(DotNetNuke.Data.DataProvider.Instance().ExecuteScalar("Nuntio_ContentLog_Add", objLog.ContentId, objLog.ModuleId, objLog.CreatedDate, objLog.CreatedBy, objLog.UserComment, objLog.Comment), Integer)

        End Function

        Public Function GetLogEntries(ByVal ModuleId As Integer, ByVal strLocale As String, ByVal strRole As String) As List(Of ContentLogInfo)

            Dim lst As New List(Of ContentLogInfo)
            lst = CBO.FillCollection(Of ContentLogInfo)(DotNetNuke.Data.DataProvider.Instance().ExecuteReader("Nuntio_ContentLog_List", ModuleId, strLocale, strRole), lst)
            Return lst

        End Function
 

#End Region

#Region "Optional Interfaces"

        Public Function GetSearchItems(ByVal ModInfo As Entities.Modules.ModuleInfo) As DotNetNuke.Services.Search.SearchItemInfoCollection Implements Entities.Modules.ISearchable.GetSearchItems

            Dim SearchItemCollection As New SearchItemInfoCollection

            Dim ContentCollection As New ContentCollection
            ContentCollection = ListContent(ModInfo.ModuleID)

            Dim lstContent As New List(Of ContentInfo)
            lstContent = ContentCollection.GetContentByRole("GENERIC")

            For Each oContent As ContentInfo In lstContent

                Dim strContent As String = HtmlUtils.Clean(HttpUtility.HtmlDecode(oContent.Content), False).Trim
                Dim strDescription As String = HtmlUtils.Shorten(HtmlUtils.Clean(strContent, False), MAX_DESCRIPTION_LENGTH, "...").Trim
                Dim SearchItem As SearchItemInfo = New SearchItemInfo(ModInfo.ModuleTitle, strDescription, oContent.CreatedBy, oContent.CreatedDate, ModInfo.ModuleID, oContent.Locale, strDescription, "", Null.NullInteger)
                SearchItemCollection.Add(SearchItem)

            Next

            Return SearchItemCollection

        End Function

        Public Function ExportModule(ByVal ModuleID As Integer) As String Implements Entities.Modules.IPortable.ExportModule

            Dim strXML As String = ""
            Dim ContentCollection As New ContentCollection
            ContentCollection = ListContent(ModuleID)

            If ContentCollection.Count <> 0 Then
                strXML += "<NuntioContentList>"
                Dim objContent As ContentInfo
                For Each objContent In ContentCollection.Items
                    strXML += "<NuntioContent>"
                    strXML += "<Content>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(objContent.Content) & "</Content>"
                    strXML += "<Title>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(objContent.Title) & "</Title>"
                    strXML += "<Locale>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(objContent.Locale) & "</Locale>"
                    strXML += "<Role>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(objContent.Role) & "</Role>"
                    strXML += "<Priority>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(CType(objContent.Priority, String)) & "</Priority>"
                    strXML += "<IsPublished>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(CType(objContent.IsPublished, String)) & "</IsPublished>"
                    strXML += "<Version>" & DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(CType(objContent.Version, String)) & "</Version>"
                    strXML += "</NuntioContent>"
                Next
                strXML += "</NuntioContentList>"
            End If

            Return strXML

        End Function

        Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserId As Integer) Implements Entities.Modules.IPortable.ImportModule

            Dim xmlContent As XmlNode
            Dim xmlContentList As XmlNode = DotNetNuke.Common.Globals.GetContent(Content, "NuntioContentList")

            For Each xmlContent In xmlContentList.SelectNodes("NuntioContent")
                Dim objText As New ContentInfo
                objText.ModuleId = ModuleID
                objText.Content = xmlContent.Item("Content").InnerText
                objText.Title = xmlContent.Item("Title").InnerText
                objText.Locale = xmlContent.Item("Locale").InnerText
                objText.Role = xmlContent.Item("Role").InnerText
                objText.Priority = CType(xmlContent.Item("Priority").InnerText, Integer)
                Try
                    objText.IsPublished = CType(xmlContent.Item("IsPublished").InnerText, Boolean)
                Catch ex As Exception
                    objText.IsPublished = True
                End Try
                objText.Version = CType(xmlContent.Item("Version").InnerText, Integer)
                objText.CreatedDate = Date.Now
                objText.CreatedBy = UserId
                AddContent(objText)
            Next

        End Sub

#End Region

        Public Function UpgradeModule(ByVal Version As String) As String Implements DotNetNuke.Entities.Modules.IUpgradeable.UpgradeModule

            If Version = "04.02.00" Or Version = "04.02.01" Then
                Try
                    DotNetNuke.Data.DataProvider.Instance().ExecuteNonQuery("Nuntio_Content_UpgradeDatabase")
                Catch exc As Exception
                    Return "Module could not be upgraded properly: " & exc.Message
                End Try
            End If

            Try

                Dim path As String = HttpRuntime.AppDomainAppPath & "Desktopmodules\pnc.Publisher.Content\"
                System.IO.Directory.Delete(path, True)

                path = HttpRuntime.AppDomainAppPath & "bin\pnc.Publisher.Content.dll"
                System.IO.File.Delete(path)

            Catch
            End Try

            Return "Module upgraded sucessfully"

        End Function


    End Class

End Namespace
